---
layout: docs
page_title: file - Functions - Configuration Language
description: |-
  The file function reads the contents of the file at the given path and
  returns them as a string.
---

# `file` Function

`file` reads the contents of a file at the given path and returns them as
a string. Relative paths will be resolved based on the CLI’s current working directory.

```hcl
file(path)
```

Strings in the Nomad language are sequences of Unicode characters, so
this function will interpret the file contents as UTF-8 encoded text and
return the resulting Unicode characters. If the file contains invalid UTF-8
sequences then this function will produce an error.

Functions are evaluated by the CLI during configuration parsing rather than job run time,
so this function can only be used with files that are already present on disk on operator host.

## Examples

```shell-session
> file("./hello.txt")
Hello World
```

Or in an example job specification:

```hcl
# /path/to/example/jobspec.nomad
job "test" {
  group "test" {
    task "test" {
      driver = "docker"
      config {
        image  = "test/image:latest"

        volumes = [
          "local/file.txt:/path/to/file/on/container/file.txt"
        ]
      }

      template {
        data        = file("/path/to/example/file.txt")
        destination = "local/file.txt"
      }
    }
  }
}
```

## Related Functions

- [`fileexists`](/nomad/docs/reference/hcl2/functions/file/fileexists) determines whether a file exists
  at a given path.
